// import moduleName=require('../../../../platform/common/moduleName');
import platform = require('../../../../platform/common/baseCommon');
import bpCommon = require('../../../../platform/common/bpCommon');
import appClient = require('../../../../platform/client/appClient');

let moduleName = "platform.client";

let loginFactory = new appClient.asyncFactory(bpCommon.IUserLogin, "remoteCall", "loginService");
let login = <bpCommon.IUserLogin>loginFactory.getValue();

let sendcodeFactory = new appClient.asyncFactory(bpCommon.ISMSPlatform, "remoteCall", "getcodeService");
let sendcode = <bpCommon.ISMSPlatform>sendcodeFactory.getValue();

/**
 * 登录视图
 */
@platform.addon("登录视图", "登录视图", moduleName)
@appClient.template('views/userinfo/login.html')
export class loginView extends appClient.baseControl {
    /**
     * 获取验证码按钮
     */
    $btnGetCode?: JQuery = undefined;
    /**
     * 手机登录按钮
     */
    $btnLoginMobile?: JQuery = undefined;
    /**
     * 输入手机号文本
     */
    $txtMobile?: JQuery = undefined;
    /**
     * 输入验证码文本
     */
    $txtVerCode?: JQuery = undefined;

    /**
     * 输入用户名文本
     */
    $txtAccount?: JQuery = undefined;
    /**
     * 输入密码文本
     */
    $txtPassword?: JQuery = undefined;

    /**
     * 帐号登录按钮
     */
    $btnLoginAccount?: JQuery = undefined;
    /**
     * 验证码
     */
    verCode?: string = "";
    /**
     * 短信模板id
     */
    tpl_id?: number;
    /**
     * 验证码长度
     */
    codeLen?: number;
    /**
     * 验证码是否通过
     */
    vrcode?: boolean;
    /**
     * 验证码时间间隔
     */
    maxCount?: number = 30;

    private curCount?: number;

    InterValObj?: number;


    $qrcode?: JQuery = undefined;
    onAfterBind?() {
        let that = this;
        /**
         * 获取验证码按钮事件
         */
        this.$btnGetCode.click(() => {
            let mobile: string = this.$txtMobile.val();
            if (mobile.length < 11) {
                alert("请输入11位手机号码！");
                return;
            }
            this.curCount = this.maxCount;
            this.InterValObj = window.setInterval(() => {
                this.curCount--;
                if (this.curCount == 0) {
                    window.clearInterval(this.InterValObj);//停止计时器
                    this.$btnGetCode.text("重新获取");
                    this.$btnGetCode.removeClass("disabled");
                }
                else {
                    this.$btnGetCode.text(this.curCount + "秒后重发");
                }
            }, 1000);
            this.$btnGetCode.text(this.maxCount + "秒后重发");
            this.$btnGetCode.addClass("disabled");

            this.$btnLoginMobile.removeClass("disabled");
            try {
                // sendcode.send_tpl_sms(mobile,that.tpl_id,that.tpl_value).then((value) => {
                //     that.verCode = value;
                // });
                login.getCode(mobile, that.tpl_id, that.codeLen);
            } catch (error) {
                console.log(error.message.responseText);
                alert(JSON.parse(error.message).responseText);
            }

        });
        /**
         * 手机登录按钮事件
         */
        this.$btnLoginMobile.click(() => {
            let mobile: string = this.$txtMobile.val();
            let tempCode: string = this.$txtVerCode.val();
            login.codelogin(mobile,tempCode).then((rs) => {
                // if (value != true) {
                //     alert("输入验证码不正确！")
                //     return;
                // }
                if (rs.header.rtnCode == "01") {
                    alert(rs.header.rtnMsg.message);
                    return;
                }
                let userinfo: platform.userInfo = new platform.userInfo();
                let usertoken: platform.userToken = new platform.userToken();
                userinfo.loginType = "Mobile";
                userinfo.logingAccount = this.$txtMobile.val();
                userinfo.password = this.$txtVerCode.val();
                usertoken.userinfo = userinfo;
                try {
                    platform.currentUser = usertoken;//要注释
                    var userTK = login.login(usertoken).then(rs => {
                        let token = rs.body;
                        if (rs.header.rtnCode == "00" && token.userinfo.status == "00") {
                            platform.currentUser = token;
                            appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/userInfoViewConfig', '我的');
                        }
                        else {
                            alert(rs.header.rtnMsg.message);
                        }
                    });
                    appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/userInfoViewConfig', '我的');
                } catch (error) {
                    console.log(error.message.responseText);
                    alert(JSON.parse(error.message).responseText);
                }
            });
        });
        /**
         * 帐号登录按钮事件
         */
        this.$btnLoginAccount.click(() => {
            let userinfo: platform.userInfo = new platform.userInfo();
            let usertoken: platform.userToken = new platform.userToken();
            userinfo.loginType = "Account";
            userinfo.logingAccount = this.$txtAccount.val();
            userinfo.password = this.$txtPassword.val();
            usertoken.userinfo = userinfo;
            // let rq=new  platform.RequestMessage<platform.userToken>();
            // rq.body=usertoken;
            platform.currentUser = usertoken;//要注释
            try {
                login.login(usertoken).then(rs => {
                    let token = rs.body;
                    if (rs.header.rtnCode == "00" && token.userinfo.status == "00") {
                        platform.currentUser = token;
                        appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/userInfoViewConfig', '我的');
                    }
                    if (rs.header.rtnCode == "01") {
                        alert(rs.header.rtnMsg.message);
                    }
                });
            } catch (error) {
                //console.log(error.message.responseText);
                alert("登录失败：" + JSON.parse(error.message).responseText);
            }

        });
    };
}